<?php
/** User
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Setup 
	* @author Ray Nowell 
	*/  
class User extends MY_Controller {

	function User()
	{
		parent::MY_Controller();
	}
	
	//Private Functions:
	function _get_form_post_data($add_record = false)
	{

		$data = array('id' 		=>	$this->input->post('id'),
						'username'		=>	$this->input->post('username'),
						'first_name'	=>	$this->input->post('first_name'),
						'last_name'		=>	$this->input->post('last_name'),
						'email'			=>	$this->input->post('email'),
						'group_id'		=>	$this->input->post('group_id'),
						'facility_id'	=>	$this->input->post('facility_id'),
					);
    
    if ($add_record)
        array_shift($data);
	
		return $data;
	}
	
	function _get_additional_javascript()
	{
		$ret_val = '$("#_group").focus();';
		return $ret_val; 
	}
	
	function _get_group_lookup()
	{
		////////////////////////
		//Store look-up values//
		////////////////////////
		$group_lookup = $this->User_model->get_user_group_lookup();		
		return set_lookup($group_lookup,'id','name',BLANK_ROW);
		
	}
	
	function _get_facility_lookup()
	{
		////////////////////////
		//Store look-up values//
		////////////////////////
		$facility_lookup = $this->User_model->get_facility_lookup();		
		return set_lookup($facility_lookup,'facility_id','facility_name',BLANK_ROW);
		
	}
	
	function add()
	{
		//$this->output->enable_profiler(TRUE);
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model('User_model');

		$data['title'] = COMPANY_NAME." -- Add User ";
		$data['heading'] = "User Form";
	
		//Set the values for the User View
		$data['id']		=	'';
		$data['username']		=	'';
		$data['first_name']		=	'';
		$data['last_name']		=	'';
		$data['email']			=	'';
		$data['group_id']		=	'';
		$data['facility_id']	=	'';
		
		$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript();
		
		$data['save_type'] = 'save_new';
				
		$this->load->view('user_view', $data);	 
		
	}
	 
	function edit()
	{
		$this->load->helper('date');
		$this->load->model('User_model');
		
		//$this->output->enable_profiler(TRUE); 

		$data['title'] = COMPANY_NAME." -- User Edit";
		$data['heading'] = "User Edit";
		
		//The number of segments will determine the edit mode of the intervention:	single-list or multi-record navigation
		$total_segments = $this->uri->total_segments();
		
		if ($total_segments == SINGLE_EDIT)
		{
			$id = $this->uri->segment(3, 0);	//Three is the Intervention Id.	zero is the default value
			$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript().get_save_javascript('user');
		}			
		else 
		{
			$segment_to_get = $this->uri->segment(3);
				
			$id = $this->uri->segment($segment_to_get);
			
			$previous_segment = $segment_to_get <= 4 ? 4 : $segment_to_get - 1;
			$next_segment = $segment_to_get == $total_segments	? $total_segments : $segment_to_get + 1;
			$fullUri = $this->uri->uri_string();
			$search_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
			$replace_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'|';
			$current_uri = str_replace($search_text, $replace_text, $fullUri);
			$nav_url = base_url().'index.php'.$current_uri;
			$data['additional_javascript_doc_ready_functions'] = get_navigation_doc_ready_javascript().$this->_get_additional_javascript().get_save_javascript('user');
			$data['additional_jquery_javascript'] = get_navigation_functions($nav_url, $previous_segment, $next_segment, $total_segments);
			$data['nav_bar'] = get_navigation_markup(base_url(), $segment_to_get - 3, $total_segments - 3);
			
		}

		$user_row = $this->User_model->get_user_id_record($id);
		
		//Set the values for the User View
		
		$data['id']	=	$user_row->id;
		
		$data['id']		=	$user_row->id;
		$data['username']		=	$user_row->username;
		$data['first_name']		=	$user_row->first_name;
		$data['last_name']		=	$user_row->last_name;
		$data['email']			=	$user_row->email;
		$data['group_id']		=	$user_row->group_id;
		$data['facility_id']	=	$user_row->facility_id;
		$data['created_date']	=	$user_row->created_date;
		$data['modified_date']	=	$user_row->modified_date;
		
		
		$data['user_group_list'] 	=	$this->_get_group_lookup();
		$data['facility_list'] 		=	$this->_get_facility_lookup();

		$data['save_type'] = 'update';
				
		$this->load->view('user_view', $data);	 
	}
		
	/** 
	 * Calls the User Model to save the user based on the post data passed
	 * @access public */
	function save_new()
	{
		$this->load->model('User_model');
				
		$data = $this->_get_form_post_data(true);
		
		$data['created_date'] = date('Y-m-d H:i:s');
		$data['created_by'] = $this->ion_auth->get_user()->username;
		
		$this->User_model->Add_user($data);
		redirect('user/add');
	}
	
	/** 
	 * Calls the User Model to update the user based on the post data passed
	 * @access public */
	function update()
	{
		$this->load->model('User_model');
		$data = $this->_get_form_post_data();
		$this->User_model->Update_user($data);
		redirect('user/edit/'.element('id', $data));
	}
}

/* End of file user.php */
/* Location: ./system/application/controllers/user.php */
